package ar.com.santiagobasulto.crawler.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.ContentHandler;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.text.html.HTMLEditorKit;

import ar.com.santiagobasulto.crawler.guy.HTMLParse;
import ar.com.santiagobasulto.crawler.test.EmailParser;

public class Test {
	public static void printStuff(String urlString){
		try {
			URL url = new URL(urlString);
			URLConnection con = url.openConnection();

			Map<String, List<String>> map = con.getHeaderFields();
			Set<String> keys = map.keySet();
			Iterator<String> it = keys.iterator();
			while(it.hasNext()){
				String key = it.next();
				Iterator<String> listIterator = map.get(key).iterator();
				System.out.println("KEY: "+key);
				while(listIterator.hasNext()){
					String listElement = listIterator.next();
					System.out.println("\t"+listElement);
				}
			}
			InputStream in = con.getInputStream();
			Scanner n = new Scanner(in);
			while(n.hasNext()){
				System.out.println(n.nextLine());;
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void doMoreStuff(String urlString){
		try {
			URL url = new URL(urlString);
			URLConnection con = url.openConnection();

			InputStream in = con.getInputStream();
		      Reader r = new InputStreamReader(in);
		      // parse the URL
		      HTMLEditorKit.Parser parse = new HTMLParse().getParser();
		      //parse.parse(r,new Parser(url),true);
		      parse.parse(r,new EmailParser(url),true);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//doMoreStuff("http://localhost/");
		Runnable r = new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("Running");
				while(true){
				}
			}
		};
		ExecutorService pool = Executors.newFixedThreadPool(100);
		for (int i = 0; i < 100; i++) {
			pool.submit(r);
		}
	}
}
